package com.aforce.appearances;

import java.io.File;
import java.net.MalformedURLException;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.PointAttributes;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Color3f;

import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;


public class BoxAppearance{
	private static java.net.URL texImage = null;
	private static java.net.URL bgImage = null;
	
	
	public BoxAppearance(){
		if (bgImage == null) {
			// the path to the image for an applet
			try {
				bgImage = new File("resources/images/bg.jpg").toURI().toURL();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (bgImage == null) {
				System.err.println("resources/images/bg.jpg not found");
				System.exit(1);
			}
		}

		if (texImage == null) {
			// the path to the image for an applet
			try {
				texImage = new File("resources/images/stone.jpg").toURI()
						.toURL();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (texImage == null) {
				System.err.println("resources/images/stone.jpg not found");
				System.exit(1);
			}
		}
	}
	
	public Appearance createAppearance(int idx) {
		Appearance app = new Appearance();

		// Globally used colors
		Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
		Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

		switch (idx) {
		// Unlit solid
		case 0: {
			// Set up the coloring properties
			Color3f objColor = new Color3f(1.0f, 0.2f, 0.4f);
			ColoringAttributes ca = new ColoringAttributes();
			ca.setColor(objColor);
			app.setColoringAttributes(ca);
			break;
		}

			// Unlit wire frame
		case 1: {
			// Set up the coloring properties
			Color3f objColor = new Color3f(0.5f, 0.0f, 0.2f);
			ColoringAttributes ca = new ColoringAttributes();
			ca.setColor(objColor);
			app.setColoringAttributes(ca);

			// Set up the polygon attributes
			PolygonAttributes pa = new PolygonAttributes();
			pa.setPolygonMode(pa.POLYGON_LINE);
			pa.setCullFace(pa.CULL_NONE);
			app.setPolygonAttributes(pa);
			break;
		}

			// Unlit points
		case 2: {
			// Set up the coloring properties
			Color3f objColor = new Color3f(0.2f, 0.2f, 1.0f);
			ColoringAttributes ca = new ColoringAttributes();
			ca.setColor(objColor);
			app.setColoringAttributes(ca);

			// Set up the polygon attributes
			PolygonAttributes pa = new PolygonAttributes();
			pa.setPolygonMode(pa.POLYGON_POINT);
			pa.setCullFace(pa.CULL_NONE);
			app.setPolygonAttributes(pa);

			// Set up point attributes
			PointAttributes pta = new PointAttributes();
			pta.setPointSize(5.0f);
			app.setPointAttributes(pta);
			break;
		}

			// Lit solid
		case 3: {
			// Set up the material properties
			Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
			app.setMaterial(new Material(objColor, black, objColor, white,
					80.0f));
			break;
		}

			// Texture mapped, lit solid
		case 4: {
			// Set up the texture map
			/*
			TextureLoader tex = new TextureLoader(texImage, this);
			app.setTexture(tex.getTexture());

			TextureAttributes texAttr = new TextureAttributes();
			texAttr.setTextureMode(TextureAttributes.MODULATE);
			app.setTextureAttributes(texAttr);

			// Set up the material properties
			app.setMaterial(new Material(white, black, white, black, 1.0f));
			break;
			*/
			
			Material m = new Material(new Color3f(0.6f, 0.3f, 0.0f), new Color3f(0.6f, 0.3f, 0.0f), new Color3f(0.6f, 0.3f, 0.0f), new Color3f(0.6f, 0.3f, 0.0f), 100.0f);
			ColoringAttributes ca = new ColoringAttributes();
			ca.setColor(0.6f, 0.3f, 0.0f);
			//app.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
			app.setColoringAttributes(ca);
			m.setLightingEnable(true);
			app.setMaterial(m);
			break;
		}

			// Transparent, lit solid
		case 5: {
			// Set up the transparency properties
			TransparencyAttributes ta = new TransparencyAttributes();
			ta.setTransparencyMode(ta.BLENDED);
			ta.setTransparency(0.6f);
			app.setTransparencyAttributes(ta);

			// Set up the polygon attributes
			PolygonAttributes pa = new PolygonAttributes();
			pa.setCullFace(pa.CULL_NONE);
			app.setPolygonAttributes(pa);

			// Set up the material properties
			Color3f objColor = new Color3f(0.7f, 0.8f, 1.0f);
			app
					.setMaterial(new Material(objColor, black, objColor, black,
							1.0f));
			break;
		}

			// Lit solid, no specular
		case 6: {
			// Set up the material properties
			Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
			app.setMaterial(new Material(objColor, black, objColor, black,
					80.0f));
			break;
		}

			// Lit solid, specular only
		case 7: {
			// Set up the material properties
			Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
			app.setMaterial(new Material(black, black, black, white, 80.0f));
			break;
		}

			// Another lit solid with a different color
		case 8: {
			// Set up the material properties
			Color3f objColor = new Color3f(0.8f, 0.8f, 0.0f);
			app.setMaterial(new Material(objColor, black, objColor, white,
					80.0f));
			break;
		}
		case 9: {
			// Set up the material properties
			Color3f objColor = new Color3f(0.2f, 0.2f, 0.2f);
			app.setMaterial(new Material(objColor, black, objColor, white,
					80.0f));
			break;
		}
		
		case 10: {
			// Set up the material properties
			Color3f objColor = new Color3f(0.4f, 0.2f, 0.7f);
			Material m = new Material(objColor, black, objColor, white, 80.0f);
			m.setShininess(128);
			app.setMaterial(m);
			break;
		}
		
		case 11: {
			// Set up the material properties
			Color3f objColor = new Color3f(0.4f, 0.7f, 0.2f);
			Material m = new Material(objColor, black, objColor, white, 80.0f);
			m.setShininess(0);
			app.setMaterial(m);
			break;
		}
		

		default: {
			ColoringAttributes ca = new ColoringAttributes();
			ca.setColor(new Color3f(0.0f, 1.0f, 0.0f));
			app.setColoringAttributes(ca);
		}
		}
		
		app.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
		app.setCapability(Appearance.ALLOW_POINT_ATTRIBUTES_WRITE );
		app.setCapability(Appearance.ALLOW_POLYGON_ATTRIBUTES_WRITE);
		
		return app;
	}
}